package cc.vv.baselibrary.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import cc.vv.BaseNewApplication;
import cc.vv.baselibrary.bean.request.SaveUserInfoRequest;
import cc.vv.baselibrary.global.UserInfoSharePreKey;
import cc.vv.lklibrary.log.LogOperate;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 用户信息管理操作类
 * Created by sun.li on 2017/7/27.
 */

public class UserInfoManageUtil {
    /*用户账号类型:手机号 2；*/
    public static final String UserLoginType_PhoneCode = "2";

    /*用户账号类型:邮箱 1；第三方微信3*/
    public static final String UserLoginType_MailCode = "1";

    /*用户账号类型:第三方微信 3*/
    public static final String UserLoginType_WeChatCode = "3";

    /**
     * 获取用户ID
     */
    public static String getUserId() {
        String userId = "";
        UserInfoObj userInfoObj = UserInfoManageUtil.getUserInfo();
        if (userInfoObj != null) {
            try {
                userId = UserInfoManageUtil.getUserInfo().getUserId();
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
                userId = "";
            }
        }
        return userId;
    }

    /**
     * 获取Token
     */
    public static String getToken() {
        String token;
        token = LKPrefUtil.getString(UserInfoSharePreKey.TOKEN, "");
        if (TextUtils.isEmpty(token)) {
            //商城不登录的情况下使用的服务器的假token  注意：不允许删除
            token = "002a7774dd1641feadfd9292";
        }
        return token;
    }


    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserInfoObj getUserInfo() {
        UserInfoObj userInfoObj;
        try {
            userInfoObj = LKPrefUtil.getObject(UserInfoSharePreKey.USERINFO_DATA, new UserInfoObj());
        } catch (Exception e) {
            LogOperate.e(e.getMessage(), e);
            userInfoObj = null;
        }
        return userInfoObj;
    }

    /**
     * 存储用户信息数据
     *
     * @param userInfoObj 用户信息对象数据
     */
    public static void saveUserInfo(UserInfoObj userInfoObj) {
        if (null != userInfoObj) {
            LKPrefUtil.putObject(UserInfoSharePreKey.USERINFO_DATA, userInfoObj);
            LKPrefUtil.putString(UserInfoSharePreKey.TOKEN, userInfoObj.getToken());
        }
    }

    /**
     * 获取设备唯一标识IMEI
     */
    @SuppressLint("MissingPermission")
    public static String getIMEI() {
        if (BaseNewApplication.getApplication() != null) {
            String szImei = "";
            try {
                TelephonyManager TelephonyMgr = (TelephonyManager) BaseNewApplication.getApplication().getSystemService
                        (TELEPHONY_SERVICE);
                if (ActivityCompat.checkSelfPermission(BaseNewApplication.getApplication(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    return "";
                }
                szImei = TelephonyMgr.getDeviceId();
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
            }
            if (!TextUtils.isEmpty(szImei)) {
                return MD5.md5(szImei);
            }
        }
        return "";
    }


    /**
     * 初始化用户提交保存信息
     */
    public static SaveUserInfoRequest initSaveUserInfoRequest(UserInfoObj userInfoObj) {
        SaveUserInfoRequest saveUserInfoRequest = new SaveUserInfoRequest();
        if (userInfoObj != null) {
            if (!TextUtils.isEmpty(userInfoObj.getId())) {
                saveUserInfoRequest.setId(userInfoObj.getId());
            }
            if (!TextUtils.isEmpty(userInfoObj.getUserId())) {
                saveUserInfoRequest.setUserId(userInfoObj.getUserId());
            }
            if (!TextUtils.isEmpty(userInfoObj.getName())) {
                saveUserInfoRequest.setName(userInfoObj.getName());
            }
            if (!TextUtils.isEmpty(userInfoObj.getNickName())) {
                saveUserInfoRequest.setNickName(userInfoObj.getNickName());
            }
            if (!TextUtils.isEmpty(userInfoObj.getLargeImg())) {
                saveUserInfoRequest.setLargeImg(userInfoObj.getLargeImg());
            }
            if (!TextUtils.isEmpty(userInfoObj.getSmallImg())) {
                saveUserInfoRequest.setSmallImg(userInfoObj.getSmallImg());
            }
            if (!TextUtils.isEmpty(userInfoObj.getPhone())) {
                saveUserInfoRequest.setPhone(StringProcessingUtil.stringRemoveBlankSpace(userInfoObj.getPhone()));
            }
            if (userInfoObj.getHeight() >= 0) {
                saveUserInfoRequest.setHeight(userInfoObj.getHeight());
            }
            if (userInfoObj.getWeight() >= 0) {
                saveUserInfoRequest.setWeight(userInfoObj.getWeight());
            }
            if (!TextUtils.isEmpty(userInfoObj.getProvinceId())) {
                saveUserInfoRequest.setProvinceId(userInfoObj.getProvinceId());
            }
            if (!TextUtils.isEmpty(userInfoObj.getCityId())) {
                saveUserInfoRequest.setCityId(userInfoObj.getCityId());
            }
            if (!TextUtils.isEmpty(userInfoObj.getProvinceId())) {
                saveUserInfoRequest.setProvinceId(userInfoObj.getProvinceId());
            }
            if (!TextUtils.isEmpty(userInfoObj.getMarriage())) {
                saveUserInfoRequest.setMarriage(userInfoObj.getMarriage());
            }
        }
        return saveUserInfoRequest;
    }

    /**
     * 获取坂本明
     *
     * @return
     */
    public static String getVersionName(Context context) {
        if (context == null) {
            return "1.0.0";
        }
        String versionName = "1.0.0";
        try {
            PackageManager manager = context.getPackageManager();
            //第二个参数代表额外的信息，例如获取当前应用中的所有的Activity
            PackageInfo packageInfo = manager.getPackageInfo(getPackageName(context), PackageManager.GET_ACTIVITIES);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            LKLogUtil.e(e.getMessage(), e);
        }
        LKLogUtil.e("VersionName：" + versionName);
        return versionName;
    }

    /**
     * 获取应用包名
     *
     * @return
     */
    private static String getPackageName(Context context) {
        String packageName = "tech.yunjing.biconlife";
        if (context != null) {
            try {
                packageName = context.getApplicationContext().getPackageName();
            } catch (Exception e) {
                LKLogUtil.e(e.getMessage(), e);
                packageName = "tech.yunjing.biconlife";
            }
        }
        LKLogUtil.e("PackageName：" + packageName);
        return packageName;
    }

    public static boolean getUserIsLogin() {
        if (TextUtils.isEmpty(getUserId())) {
            return false;
        }
        return true;
    }


    /**
     * 获取版本号
     *
     * @return
     */
    public static int getVersionCode(Context context) {
        if (context == null) {
            return 0;
        }
        int code = 0;
        try {
            //获取包管理器
            PackageManager manager = context.getPackageManager();
            //通过当前的包名获取包的信息
            PackageInfo info = manager.getPackageInfo(getPackageName(context), 0);
            if (info != null) {
                code = info.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            LKLogUtil.e(e.getMessage(), e);
        }
        return code;
    }


}
